package com.taiyi.renaissancemall.ui.mine.order

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.ui.mine.order.completed.CompletedFragment
import com.taiyi.renaissancemall.ui.mine.order.delivered.DeliveredFragment
import com.taiyi.renaissancemall.ui.mine.order.order_evaluation.CommentFragment
import com.taiyi.renaissancemall.ui.mine.order.order_sale_list.SaleFragment
import com.taiyi.renaissancemall.ui.mine.order.pendingpayment.PendingPaymentFragment
import com.taiyi.renaissancemall.ui.mine.order.received.ReceivedFragment
import kotlinx.android.synthetic.main.activity_my_order.*
import kotlinx.android.synthetic.main.base_top_black_background_layout.backIv
import kotlinx.android.synthetic.main.base_top_black_background_layout.titleTv
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class MyOrderActivity : BaseActivity<MyOrderView, MyOrderPresent>() {

    var titleList = mutableListOf<String>()
    var fragmentList = mutableListOf<Fragment>(
        PendingPaymentFragment(), DeliveredFragment(),
        ReceivedFragment(), CommentFragment(), CompletedFragment()
    )

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).statusBarDarkFont(true, 1.0f).init()
    }

    inner class MyAdapter() : FragmentStatePagerAdapter(supportFragmentManager) {

        override fun getItem(position: Int): Fragment {
            return fragmentList[position]
        }

        override fun getCount(): Int = titleList.size

        override fun getPageTitle(position: Int): CharSequence? {
            return titleList[position]
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_my_order

    override fun initPresenter(): MyOrderPresent = MyOrderPresent()
    override fun initViews(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        titleTv.text = getString(R.string.myOrder)
        titleList.run {
            add(getString(R.string.pendingPayment))
            add(getString(R.string.toBeDelivered))
            add(getString(R.string.toBeReceived))
            add(getString(R.string.comment))
            add(getString(R.string.completed))
        }
        viewPager.adapter = MyAdapter()
        tabLayout.setupWithViewPager(viewPager)
        viewPager.currentItem = intent.getIntExtra("type", 0)
    }

    override fun initData() {

    }

    @Subscribe
    fun changePageTo(s: String) {
        if (s.startsWith("changePage")) {
            viewPager.currentItem = 3
        }
    }

    override fun initEvent() {
        backIv.setOnClickListener { finish() }
    }

}
